package com.karson.front.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.karson.common.model.WatchLater;
import com.karson.common.request.AlbumSearchRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.request.WatchLaterRequest;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.response.RepliesResponse;
import com.karson.common.response.WatchLaterResponse;
import com.karson.service.service.WatchLaterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/later")
@Tag(name = "稍后再看接口")
public class WatchLaterController {
    @Autowired
    private WatchLaterService watchLaterService;
    @Operation(summary = "游标分页列表")
    @GetMapping("/cursor/list")
    public CommonResult<WatchLaterResponse> getList(@RequestParam(required = false) String offset) throws JsonProcessingException {
        WatchLaterResponse repliesResponse = watchLaterService.offsetList(offset);
        return CommonResult.success(repliesResponse);
    }
    @Operation(summary = "添加至稍后再看")
    @PostMapping("/add/{albumId}")
    public CommonResult<String> save (@PathVariable Integer albumId) {
        if (albumId != null && albumId < 0) {
            return CommonResult.failed();
        }
        Integer uid = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<WatchLater> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WatchLater::getAlbumId, albumId);
        queryWrapper.eq(WatchLater::getUserId, uid);
        WatchLater exits = watchLaterService.getOne(queryWrapper);
        if (exits != null) {
            // 如果已经在列表中了就不需要再次添加
            return CommonResult.success();
        }
        WatchLater watchLater = new WatchLater();
        watchLater.setAlbumId(albumId);
        watchLater.setUserId(uid);
        boolean save = watchLaterService.save(watchLater);
        return save ? CommonResult.success("ok") : CommonResult.failed();
    }
    @Operation(summary = "从列表中删除")
    @DeleteMapping("/del/{albumId}")
    public CommonResult<String> del(@PathVariable Integer albumId) {
        if (albumId != null && albumId < 0) {
            return CommonResult.failed();
        }
        // 根据用户id 以及 专辑id 进行删除操作
        Integer uid = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<WatchLater> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WatchLater::getAlbumId, albumId);
        queryWrapper.eq(WatchLater::getUserId, uid);
        boolean remove = watchLaterService.remove(queryWrapper);
        return remove ? CommonResult.success("ok") : CommonResult.failed();
    }
    @Operation(summary = "获取当前用户所有稍后再听列表")
    @GetMapping("/getAllList")
    public CommonResult<List<Integer>> getAllList() {
        int uid = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<WatchLater> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WatchLater::getUserId, uid);
        List<WatchLater> list = watchLaterService.list(queryWrapper);
        List<Integer> albumIds = list.stream().map(WatchLater::getAlbumId).toList();
        return CommonResult.success(albumIds);
    }
}